package com.amos.cmp_android.presenter;

import android.content.Context;

import com.amos.cmp_android.constant.URLConstant;
import com.amos.cmp_android.model.AcademyModel;
import com.amos.cmp_android.model.DetachmentModel;
import com.amos.cmp_android.model.FunctionDepartmentModel;
import com.amos.cmp_android.model.SubDepartmentModel;
import com.amos.cmp_android.net.STError;
import com.amos.cmp_android.net.STHttpCallback;
import com.amos.cmp_android.net.STNetTask;
import com.amos.cmp_android.net.STRequest;
import com.amos.cmp_android.utils.GsonUtil;
import com.orhanobut.logger.Logger;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tlh on 2016/4/11.
 */
public class GetFunctionInfoPresenter extends BasePresenter {

    private List<Object> list;
    private GetFunctionInfoListener listener;

    private int index;
    public GetFunctionInfoPresenter(Context context) {
        super(context);
    }
    public void setListener(GetFunctionInfoListener listener, List<Object> list) {
        this.listener = listener;
        this.list = list;
    }
    public void getInfo(int page,int num){
        Map<String, Object> params = new HashMap<>();
        STRequest request;
        params.put("page", page);
        params.put("num", num);
        list.clear();
        request = new STRequest(URLConstant.GET_FUNTION_DEPARTMENT, params, null);
        STNetTask.post(request, new STHttpCallback<JSONObject>() {
            @Override
            public void onSuccess(String response) {
                Logger.json(response);
                GsonUtil.fromJson(response,list,FunctionDepartmentModel.class);
                index++;
                if (index == 4 && listener != null)
                    listener.onSuccess();
            }
            @Override
            public void onFailed(STError error) {
                if (listener != null)
                    listener.onFailed(error);
            }
        }, beforeQueryHook, afterQueryHook);
        request = new STRequest(URLConstant.GET_DETACHMENT, params, null);
        STNetTask.post(request, new STHttpCallback<JSONObject>() {
            @Override
            public void onSuccess(String response) {
                Logger.json(response);
                GsonUtil.fromJson(response, list, DetachmentModel.class);
                index++;
                if (index == 4 && listener != null)
                    listener.onSuccess();
            }
            @Override
            public void onFailed(STError error) {
                if (listener != null)
                    listener.onFailed(error);
            }
        }, beforeQueryHook, afterQueryHook);

        request = new STRequest(URLConstant.GET_SUB_DEPARTMENT, params, null);
        STNetTask.post(request, new STHttpCallback<JSONObject>() {
            @Override
            public void onSuccess(String response) {
                Logger.json(response);
                GsonUtil.fromJson(response,list,SubDepartmentModel.class);
                index++;
                if (index == 4 && listener != null)
                    listener.onSuccess();
            }
            @Override
            public void onFailed(STError error) {
                if (listener != null)
                    listener.onFailed(error);
            }
        }, beforeQueryHook, afterQueryHook);

        request = new STRequest(URLConstant.GET_ACADEMY, params, null);
        STNetTask.post(request, new STHttpCallback<JSONObject>() {
            @Override
            public void onSuccess(String response) {
                Logger.json(response);
                GsonUtil.fromJson(response,list,AcademyModel.class);
                index++;
                if (index == 4 && listener != null)
                    listener.onSuccess();
            }
            @Override
            public void onFailed(STError error) {
                if (listener != null)
                    listener.onFailed(error);
            }
        }, beforeQueryHook, afterQueryHook);
    }
    public interface GetFunctionInfoListener{
        void onSuccess();
        void onFailed(STError error);
    }
}
